package net.oschina.app.ui;

import net.oschina.app.AppContext;
import net.oschina.app.AppException;
import net.oschina.app.R;
import net.oschina.app.bean.CommentList;
import net.oschina.app.bean.Result;
import net.oschina.app.common.StringUtils;
import net.oschina.app.common.UIHelper;
import net.oschina.app.widget.LinkView;
import org.holoeverywhere.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

/**
 * 发表评论
 * @author liux (http://my.oschina.net/liux)
 * @version 1.0
 * @created 2012-3-21
 */
public class CommentPub extends BaseActivity{

	public final static int CATALOG_NEWS = 1;
	public final static int CATALOG_POST = 2;
	public final static int CATALOG_TWEET = 3;
	public final static int CATALOG_ACTIVE = 4;
	public final static int CATALOG_MESSAGE = 4;//动态与留言都属于消息中心
	public final static int CATALOG_BLOG = 5;
	
	private EditText mContent;
	private CheckBox mZone;
	private LinkView mQuote;
    private ProgressDialog mProgress;
	
	private int _catalog;
	private int _id;
	private int _uid;
	private String _content;
	private int _isPostToMyZone;
	
	//-------对评论回复还需加2变量------
	private int _replyid;//被回复的单个评论id
	private int _authorid;//该评论的原始作者id
	private ActionBar mActionBar;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initActionBar();

		setContentView(R.layout.comment_pub);
		
		this.initView();
		
	}
	private void initActionBar() {
		mActionBar = getSupportActionBar();
		mActionBar.setDisplayHomeAsUpEnabled(true);
		mActionBar.setHomeButtonEnabled(true);
	}
    //初始化视图控件
    private void initView()
    {
		_id = getIntent().getIntExtra("id", 0);
		_catalog = getIntent().getIntExtra("catalog", 0);
		_replyid = getIntent().getIntExtra("reply_id", 0);
		_authorid = getIntent().getIntExtra("author_id", 0);
    	
    	mContent = (EditText)findViewById(R.id.comment_pub_content);
    	mZone = (CheckBox)findViewById(R.id.comment_pub_zone);
    	if(_catalog == CommentList.CATALOG_TWEET){
    		mZone.setVisibility(View.VISIBLE);
    	}
    	
    	mQuote = (LinkView)findViewById(R.id.comment_pub_quote);
    	mQuote.setText(UIHelper.parseQuoteSpan(getIntent().getStringExtra("author"),getIntent().getStringExtra("content")));
    	mQuote.parseLinkText();
    }
	
	private void publish() {
		_content = mContent.getText().toString();
		if(StringUtils.isEmpty(_content)){
			UIHelper.ToastMessage(this, "请输入评论内容");
			return;
		}
		
		final AppContext ac = (AppContext)getApplication();
		if(!ac.isLogin()){
			UIHelper.showLoginDialog(CommentPub.this);
			return;
		}
		
		if(mZone.isChecked())
			_isPostToMyZone = 1;
			
		_uid = ac.getLoginUid();
		
    	mProgress = ProgressDialog.show(this, null, "发表中···",true,true); 			
		
		final Handler handler = new Handler(){
			public void handleMessage(Message msg) {
				if(mProgress!=null)mProgress.dismiss();
				if(msg.what == 1){
					Result res = (Result)msg.obj;
					UIHelper.ToastMessage(CommentPub.this, res.getErrorMessage());
					if(res.OK()){
						//发送通知广播
						if(res.getNotice() != null){
							UIHelper.sendBroadCast(CommentPub.this, res.getNotice());
						}
						//返回刚刚发表的评论
						Intent intent = new Intent();
						intent.putExtra("COMMENT_SERIALIZABLE", res.getComment());
						setResult(RESULT_OK, intent);
						//跳转到文章详情
						finish();
					}
				}
				else {
					((AppException)msg.obj).makeToast(CommentPub.this);
				}
			}
		};
		new Thread(){
			public void run() {
				Message msg = new Message();
				Result res = new Result();
				try {
					//发表评论
					if(_replyid == 0){
						res = ac.pubComment(_catalog, _id, _uid, _content, _isPostToMyZone);
					}
					//对评论进行回复
					else if(_replyid > 0){
						if(_catalog == CATALOG_BLOG)
							res = ac.replyBlogComment(_id, _uid, _content, _replyid, _authorid);
						else
							res = ac.replyComment(_id, _catalog, _replyid, _authorid, _uid, _content);
					}
					msg.what = 1;
					msg.obj = res;
	            } catch (AppException e) {
	            	e.printStackTrace();
					msg.what = -1;
					msg.obj = e;
	            }
				handler.sendMessage(msg);
			}
		}.start();
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuItem item = menu.add(R.string.menu_title_search).setIcon(
				R.drawable.ic_action_send_now);
		MenuItemCompat.setShowAsAction(item,
				MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
		return true;
	}
	/**
	 * 处理menu的事件
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
		int item_id = item.getItemId();
		switch (item_id) {
		case android.R.id.home:
			finish();
			break;
		default:
			publish();
		}
		return true;
	}
}
